import { createApp } from 'vue'
import App from '@/App.vue'


// 引入vue-router4
import router from "@/router/index.ts";
// 引入 pinia
import {createPinia, Pinia} from 'pinia'
// 引入 element-plus
import locale from "element-plus/es/locale/lang/zh-cn";
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// 引入 axios实例

// import {API as API3} from '@/axios/axios_manage.ts'

// 引入pinia持久化工具
// @ts-ignore
import piniaPersist from 'pinia-plugin-persist'
import {RouteLocationNormalized} from "vue-router";

const app: any = createApp(App as any)

// vue-router相关配置

// 以下网址被忽略
const ignore_path: string[] = ['login', 'unauthorized', '404']

import {useStore} from '@/pinia'

const pinia: Pinia = createPinia()
pinia.use(piniaPersist)

app.use(ElementPlus, {locale})
app.use(pinia)
app.use(router)
app.mount('#app')

const store = useStore()





// 全局前置守卫
// @ts-ignore
router.beforeEach((to: RouteLocationNormalized) => {
    // 如果要前往的页面是ignore_path中的三个页面，那么直接放行
    if(ignore_path.includes(<string>to.name)){
        return true
    }

    // 如果此时存在token，那么直接放行，否则清空pinia，送往未授权界面
    if(localStorage.getItem('token') || sessionStorage.getItem('token')){
        return true
    }else{
        store.$reset()
        return {name:'unauthorized'}
    }
})
